#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@author: Lénia Andrade nº 5607
'''

from BeautifulSoup import BeautifulSoup
import urllib2
import wx


class TagsCounter:
    
    '''
    This method return the amount of existing html tags in the url provided by user
    '''
    def counter(self, url, tag):
        
        
        try:
            content = urllib2.urlopen(url)
            
        except:
            box = wx.MessageDialog(None, "CONNECTION ERROR", "TAGS COUNTER", wx.OK)
            
            if box.ShowModal() == wx.ID_OK:
                box.Destroy()
            pass
                    
            
        try:
            urlContent = content.read()
        
        except:
            box = wx.MessageDialog(None, "READING ERROR", "TAGS COUNTER", wx.OK)
            
            if box.ShowModal() == wx.ID_OK:
                box.Destroy()
            pass
            
        else:
            content.close()
            pass
        
        soup = BeautifulSoup(urlContent)
        
        htmlTags = soup.findAll(tag)
        
        return str(len(htmlTags))
        
    pass

            
            
        
            
            
            
    
    
    
